<?php


namespace app\common\lib\private_area;


use app\common\cachemodel\privatearea\PrivateAreaContentCheck;
use app\common\cachemodel\privatearea\PrivateAreaGoods;
use app\common\cachemodel\privatearea\PrivateAreaQuestionInfo;
use app\common\lib\Util;
use think\Db;
use think\Env;
use think\queue\Job;

class PrivateAreaJob
{
    /**
     * 私域H5派单 检测
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function dispatchCheck(Job $job, $jobData)
    {
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 1)
        {
            $job->delete();
            return;
        }

        $questionInfoModel = new PrivateAreaQuestionInfo();
        $goodModel = new PrivateAreaGoods();
        // 获取咨询信息
        $questionInfo = $questionInfoModel->getPrivateAreaQuestionInfo($jobData['qid'], ['astro_id', 'hash']);
        // 获取私域链接信息
        $goodInfo = $goodModel->getPrivateAreaGoodInfo($questionInfo['hash'], ['name', 'aid', 'type']);

        if ($questionInfo['astro_id'] === 0)
        {
            $env = Env::get('host.name', 'local');
            $adminName = Db::table('wechatadmin.system_admin_user')->where('id', $goodInfo['aid'])->value('username');
            Util::resDingDingMsgSend('[' . $env . ']特别提醒，订单超12小时未派单',
                "任务ID：{$jobData['qid']} \r\n" .
                "商品名称：{$goodInfo['name']} \r\n" .
                "专属客服：{$adminName}",
                Env::get('host.admin_domain') . '/admin/private_area.task/wait_task.html?id=' . $jobData['qid'], 'private_area', 3);
        }

        $job->delete();
    }

    /**
     * 私域H5    答案审核超时【12小时】检测
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function AnswerCheckJob(Job $job, $jobData)
    {
        // $jobData = [
        //     'id'=>1      // 审核表ID
        //      'good_type'=>'星盘'   // 商品类型
        //      'good_name'=>'' //  商品名称
        //      'admin_name'=>''//专属客服
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 1)
        {
            $job->delete();
            return;
        }

        $resData = (new PrivateAreaContentCheck())->where('id', $jobData['id'])->find();

        if ((int)$resData['status'] === 1)
        {// 待审核
            $env = Env::get('host.name', 'local');
            Util::resDingDingMsgSend('[' . $env . ']特别提醒，超12小时未审核',
                "任务ID：{$jobData['id']} \r\n" .
                "商品名称：{$jobData['good_name']} \r\n" .
                "专属客服：{$jobData['admin_name']}",
                Env::get('host.admin_domain') . '/admin/private_area.task/task_check_list.html?id=' . $jobData['id'], 'private_area', 9);
        }

        $job->delete();
    }

    /**
     * 私域H5    占星师【6小时】未回复 检测
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function astroReplyHour6Check(Job $job, $jobData)
    {
        // $jobData = [
        //     'qid'=>1      // 咨询ID
        //      'good_type'=>'星盘'   // 商品类型
        //      'astro_id'=>'' //  占星师ID
        //      'tel'=>'' //  手机号
        //      'start_time'=>时间戳 // 开始时间
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 1)
        {
            $job->delete();
            return;
        }

        $questionInfoModel = new PrivateAreaQuestionInfo();
        // 获取咨询信息
        $questionInfo = $questionInfoModel->getPrivateAreaQuestionInfo($jobData['qid'], ['status', 'dispatch_time', 'astro_id', 'hash']);

        if (in_array($questionInfo['status'], [4, 6], true) && $questionInfo['dispatch_time'] == $jobData['start_time'] && $questionInfo['astro_id'] == $jobData['astro_id'])
        {   // 还未完成 且 占星师ID 一致  且 派单时间相同
            // 【星座女神咨询服务】你有新的任务还未完成上传，请尽快完成上传，链接：https://wechat.goddessxzns.com/r/{others}
            Util::sendHySms([$jobData['tel']], '', 128347, ['others' => $questionInfo['hash']]);
        }

        $job->delete();
    }

    /**
     * 私域H5    占星师超时未回复【12小时】检测
     * @param Job $job
     * @param $jobData
     * @throws \Exception
     */
    public function astroReplyTimeOutCheck(Job $job, $jobData)
    {
        // $jobData = [
        //     'qid'=>1      // 咨询ID
        //      'good_type'=>'星盘'   // 商品类型
        //      'good_name'=>'' //  商品名称
        //      'admin_name'=>''//专属客服
        //      'start_time'=>时间戳 // 开始时间
        // ];
        $jobAttempts = $job->attempts();
        if ($jobAttempts > 1)
        {
            $job->delete();
            return;
        }

        $questionInfoModel = new PrivateAreaQuestionInfo();
        // 获取咨询信息
        $questionInfo = $questionInfoModel->getPrivateAreaQuestionInfo($jobData['qid'], ['status', 'last_submit_time']);

        if ($questionInfo['status'] === 4 || ($questionInfo['status'] === 6 && $questionInfo['last_submit_time'] <= $jobData['start_time']))
        {   // 还未回复 || 审核失败且上次提交答案时间小于发起监听时间
            $env = Env::get('host.name', 'local');
            Util::resDingDingMsgSend('[' . $env . ']特别提醒，占星师超12小时未回复',
                "任务ID：{$jobData['qid']} \r\n" .
                "商品名称：{$jobData['good_name']} \r\n" .
                "专属客服：{$jobData['admin_name']}",
                Env::get('host.admin_domain') . '/admin/private_area.task/my_underway_task.html?id=' . $jobData['qid'], 'private_area', 5);
        }

        $job->delete();
    }
}
